﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cotizav.CONTROLADOR;
using Cotizav.CONEXION;

namespace Cotizav.PRESENTACION.DialogBox
{
    public partial class DialogSelectObject : Form
    {
        private string nombreParametro;
        private string[] optionalParameters;
        private string idEntity;
        private CtrlFormato ctFormato;
        private CtrlContactos ctContacto;
        private CtrlCondicion ctCondicion;
        private Object result;

        public Object Result
        {
            get { return result; }
            set { result = value; }
        }
        
        public DialogSelectObject()
        {
            InitializeComponent();

            ctFormato = new CtrlFormato();
            ctContacto = new CtrlContactos();
            ctCondicion = new CtrlCondicion();
        }

        public DialogSelectObject(string parameter, params string[]  list) : this()
        {
            nombreParametro = parameter;
            optionalParameters = list;
        }

        public DialogSelectObject(string parameter, string idTable) : this()
        {
            nombreParametro = parameter;
            idEntity = idTable;
        }

        private void DialogSelectObject_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(nombreParametro))
            {
                switch (nombreParametro)
                {
                    case "Formato":
                        chkListObjects.DataSource = ctFormato.getListFormatos();
                        chkListObjects.ValueMember = "idformato";
                        chkListObjects.DisplayMember = "nombreEncabezado";
                        break;
                    case "Contacto":
                        chkListObjects.DataSource = ctContacto.getListContacto(idEntity);
                        chkListObjects.ValueMember = "idContacto";
                        chkListObjects.DisplayMember = "nombre";
                        break;
                    case "Condicion":
                        //aqui se muestran por el tipo de condicion
                        IEnumerable<CONDICION> listresult = new List<CONDICION>();
                        
                        foreach (string value in optionalParameters)
                        {
                            listresult = listresult.Union(ctCondicion.getListCondicion(value));
                        }
                        chkListObjects.DataSource = listresult.ToList<CONDICION>();
                        chkListObjects.ValueMember = "idCondicion";
                        chkListObjects.DisplayMember = "nombreCondicion";
                        break;
                }
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (chkListObjects.CheckedItems.Count > 0)
            {
                switch (nombreParametro)
                {
                    case "Formato":
                        List<FORMATO> listFormato = new List<FORMATO>();
                        foreach (Object c in chkListObjects.CheckedItems)
                        {
                           FORMATO form = c as FORMATO;
                           listFormato.Add(form);
                        }

                        Result = listFormato;
                        break;
                    case "Contacto":
                        List<CONTACTO> listContacto = new List<CONTACTO>();
                        foreach (Object c in chkListObjects.CheckedItems)
                        {
                            CONTACTO cont = c as CONTACTO;
                            listContacto.Add(cont);
                        }

                        Result = listContacto;
                        break;
                    case "Condicion":
                        List<CONDICION> listCondicion = new List<CONDICION>();
                        foreach (Object c in chkListObjects.CheckedItems)
                        {
                            CONDICION cond = c as CONDICION;
                            listCondicion.Add(cond);
                        }

                        Result = listCondicion;
                        break;
                }
            }
        }
    }
}
